package com.example.educationalsystem.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName : SwaggerConfig  //类名
 * @Description : Swagger基本配置  //描述
 * @Author : zpc20 //作者
 * @Date: 2022/12/1  10:38
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
    //扫包
    private final String BASE_PACKAGE = "com.example.educationalsystem.controller";
    // API接口页面的标题
    private final String API_TITLE = "API接口文档";
    // API版本号
    private final String API_VERSION = "1.0";
    // API描述
//    private final String API_DESCRIPTION = "请先配置token后,在调试接口";
    @Value("${keycloak-role.app}")
    private String roleApp;

    @Bean
    public Docket docket() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("Authorization").description("令牌").defaultValue("Bearer").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .select()
                //设置basePackage会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                //只有标记了@ApiOperation的方法才会暴露出给swagger
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContext());
//                .globalOperationParameters(pars);
    }

    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                // API接口页面的标题
                .title(API_TITLE)
                // API版本号
                .version(API_VERSION)
                // 描述
//                .description(API_DESCRIPTION)
                .contact(new Contact("zpc", "EducationalSystem.csdn.net", "aaa@gmail.com"))
                .license("The Apache License")
                .licenseUrl("http://www.baidu.com")
                .build();
    }
    private List<ApiKey> securitySchemes() {
        // 在请求头header添加一个名为Authorization的token
        return Collections.singletonList(new ApiKey(HttpHeaders.AUTHORIZATION, "Authorization", "header"));
    }

    private List<SecurityContext> securityContext() {
        List<SecurityContext> list = new ArrayList();
        SecurityContext securityContext = SecurityContext.builder()
                .securityReferences(securityReferences())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
        list.add(securityContext);
        return list;

    }

    List<SecurityReference> securityReferences() {

        final AuthorizationScope authorizationScope = new AuthorizationScope(this.roleApp, "Credenciais do APP (TERMINAL)");
        final AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{authorizationScope};
        return Collections.singletonList(new SecurityReference("Bearer", authorizationScopes));

    }


}
